<?php
include("check.php");
//include("check2.php");
include_once("../function.php");
include_once ("../bonus.php");
include_once('../wechatpay/WxPayPubHelper.php');
include_once("../class/system_class.php");

$_system=new system_class();
$systemparameters=$_system->system_information(1);
header("Content-Type: text/html;charset=utf-8");
session_start();

//获取来源地址
$URL['PHP_SELF'] = isset($_SERVER['PHP_SELF']) ? $_SERVER['PHP_SELF'] : (isset($_SERVER['SCRIPT_NAME']) ? $_SERVER['SCRIPT_NAME'] : $_SERVER['ORIG_PATH_INFO']);   //当前页面名称
$URL['DOMAIN'] = $_SERVER['SERVER_NAME'];  //域名(主机名)
$URL['QUERY_STRING'] = $_SERVER['QUERY_STRING'];   //URL 参数
$URL['URI'] = $URL['PHP_SELF'].($URL['QUERY_STRING'] ? "?".$URL['QUERY_STRING'] : "");
$fromurl = "http://".$URL['DOMAIN'].$URL['PHP_SELF'].($URL['QUERY_STRING'] ? '?'.$URL['QUERY_STRING'] : "");

//使用jsapi接口
$jsApi = new JsApi_pub($systemparameters);

//=========步骤1：网页授权获取用户openid============
//通过code获得openid
if (!isset($_GET['code'])){
    //触发微信返回code码
    $url = $jsApi->createOauthUrlForCode($fromurl);
    Header("Location: $url");
}else{
    //获取code码，以获取openid
    $code = $_GET['code'];
    $jsApi->setCode($code);
    $openid = $jsApi->getOpenId();
}

$uid=$_SESSION['ID'];
$orderid = trim($_GET['orderid']);

$sql = "select * from chongzhi where uid = '".$uid."' and orderid = '".$orderid."'";
$orderin = getOne($sql);
if(!$orderin){
    echo "<script language=javascript>alert('订单数据不存在');window.location.href='chongzhi.php'</script>";
    exit;
}
if($orderin['isgrant'] != 0){
    echo "<script language=javascript>alert('订单已支付');window.location.href='chongzhi.php'</script>";
    exit;
}

//订单金额
$total_fee = $orderin['jine']*100;

if($total_fee < 1){
    echo "<script language=javascript>alert('订单支付金额有误');window.location.href='chongzhi.php'</script>";
    exit;
}

if ($orderin['lx']==0){
    $body = $chongzhilx1;
}elseif($orderin['lx']==1){
    $body = $chongzhilx2;
}else{
    echo "<script language=javascript>alert('无此充值类型');window.location.href='chongzhi.php'</script>";
    exit;
}

//=========步骤2：使用统一支付接口，获取prepay_id============
//使用统一支付接口
$unifiedOrder = new UnifiedOrder_pub($systemparameters);

$unifiedOrder->setParameter("openid",$openid);//商品描述
$unifiedOrder->setParameter("body",$body);//商品描述
//自定义订单号，此处仅作举例
$unifiedOrder->setParameter("out_trade_no",$orderid);//商户订单号
$unifiedOrder->setParameter("total_fee",$total_fee);//总金额单位分
$unifiedOrder->setParameter("notify_url",'http://'.$_SERVER['HTTP_HOST'].'/mobile/wechat_notify.php');//通知地址
$unifiedOrder->setParameter("trade_type","JSAPI");//交易类型

$prepay_id = $unifiedOrder->getPrepayId();
//=========步骤3：使用jsapi调起支付============
$jsApi->setPrepayId($prepay_id);

$jsApiParameters = $jsApi->getParameters();
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <link href="css/table.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" type="text/css" href="css/style.css" />
    <link rel="stylesheet" type="text/css" href="./awesome/css/font-awesome.min.css"/>
    <link href="css/style1.css" rel="stylesheet" type="text/css">
    <link rel="stylesheet" href="./he/jquery.ui.autocomplete.css">
    <script type="text/javascript" src="./js/jquery.min.js"></script>
    <script src="./js/style.js" type="text/javascript" charset="utf-8"></script>
<title>微信充值</title>

</head>
<body>
<?php
$title="微信充值";
include_once("./public/header_nav.html");
?>
<div style="width:90%;margin:0 auto;">
    <div style="font-size:1rem;margin-top:30px;">充值类型：<?php echo $body; ?></div>
    <div style="font-size:1rem;margin-top:30px;">充值单号：<?php echo $orderid; ?></div>
    <div style="font-size:1rem;margin-top:30px;">充值金额：<?php echo $orderin['jine']; ?></div>
    <div id="paybtn" onclick="callpay()" style="margin:0 auto;margin-top:50px;border-radius:5px;width:50%;height:1.4rem;background:green;font-size:1rem;text-align: center;line-height: 1.4rem;font-weight: bold;color: #fff;">确认支付</div>
</div>
<script type="text/javascript">
    //调用微信JS api 支付
    function jsApiCall()
    {
        WeixinJSBridge.invoke(
            'getBrandWCPayRequest',
            <?php echo $jsApiParameters; ?>,
            function(res){
                WeixinJSBridge.log(res.err_msg);
                //alert(res.err_code+res.err_desc+res.err_msg);
                //alert("{$jsApiParameters}");
                if(res.err_msg == 'get_brand_wcpay_request:cancel'){
                    alert("您已取消了此次支付");

                }else if(res.err_msg == 'get_brand_wcpay_request:fail'){
                    alert("支付失败，请重新尝试");

                }else if(res.err_msg == 'get_brand_wcpay_request:ok'){
                    alert("支付成功，微信到账有一定延时，请稍候！");
                    location.href = "chongzhi.php";
                }else{
                    alert("未知错误"+res.error_msg);
                }
            }
        );
    }
    function callpay()
    {
        if (typeof WeixinJSBridge == "undefined"){
            if( document.addEventListener ){
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            }else if (document.attachEvent){
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        }else{
            jsApiCall();
        }
    }

</script>
<?php include_once ("public/footer_nav.html")?>
</body>
</html>